// pages/policy/list.js
var app = getApp();
var api = require('../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 10,
    is_manage: true,
  },
  //
  detailFun: function(e) {
    var id = e.currentTarget.dataset.id;
    if (wx.getStorageSync('real_name')) {
      wx.navigateTo({
        url: '../active/detail?id=' + id
      })
    } else {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }

  },
  fn_delete: function(e) {
    if (this.data.is_manage) {
      var id = e.currentTarget.dataset.id;
      wx.showModal({
        title: '删除提示',
        content: '确定删除该活动？',
        showCancel: true,
        cancelText: '取消',
        cancelColor: '',
        confirmText: '删除',
        confirmColor: '',
        success: (res) => {
          if (res.confirm) {
            // 接口
            app.ajax(api.active_delete, {
              rd_session: wx.getStorageSync('rd_session'),
              activity_id: id
            }, (res, err) => {
              if (res.result == "success") {
                wx.showToast({
                  title: '删除成功！',
                  icon: 'none',
                  image: '',
                  duration: 0,
                  mask: true,
                  success: (res) => {
                    this.onLoad()
                  },
                })
              }
            })
          }
        },

      })
    }
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  get_data: function() {

  },
  onLoad: function(options) {
    app.ajax(api.active_list, {
      rd_session: wx.getStorageSync('rd_session'),
      page: 1,
      page_size: this.data.page_size,
    }, (res, err) => {
      if (res.result == "success") {
        this.setData({
          list: res.data.lists,
          total_page: res.total_page,
          is_manage: wx.getStorageSync('is_manage'),
          page: 1
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var total_page = this.data.total_page;
    var page = this.data.page;
    if (page >= total_page) {
      wx.showToast({
        title: '暂无更多~',
        icon: 'none'
      })
    } else {
      page++;
      app.ajax(api.active_list, {
        rd_session: wx.getStorageSync('rd_session'),
        page: page,
        page_size: this.data.page_size,
      }, (res, err) => {
        var arr1 = this.data.list;
        var arr2 = res.data.lists;
        var arr3 = arr1.concat(arr2);
        this.setData({
          total_page: res.total_page,
          list: arr3,
          page: page
        })
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})